home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / winterp-1.13 / src-client / wl.c < prev   
Encoding:
C/C++ Source or Header  |  1991-10-06  |  6.6 KB  |  192 lines

  1. /* -*-C-*-
  2. ********************************************************************************
  3. *
  4. * File:         wl.c
  5. * RCS:          $Header: wl.c,v 1.1 91/03/14 02:51:36 mayer Exp $
  6. * Description:  Unix Domain CLIENT to send lisp expressions to WINTERP lisp-server.
  7. *        This code derived from winterp/src-client/wl-tcpip.c
  8. *        as well as code contributed by Victor Kan.
  9. * Author:       Niels Mayer, HPLabs, Victor Kan <kan@DG-RTP.DG.COM>
  10. * Created:      Sat Jun 10 02:15:35 1989
  11. * Modified:     Sat Oct  5 22:44:31 1991 (Niels Mayer) mayer@hplnpm
  12. * Language:     C
  13. * Package:      N/A
  14. * Status:       X11r5 contrib tape release
  15. *
  16. * WINTERP Copyright 1989-1991 Hewlett-Packard Company (by Niels Mayer).
  17. * XLISP version 2.1, Copyright (c) 1989, by David Betz.
  18. *
  19. * Permission to use, copy, modify, distribute, and sell this software and its
  20. * documentation for any purpose is hereby granted without fee, provided that
  21. * the above copyright notice appear in all copies and that both that
  22. * copyright notice and this permission notice appear in supporting
  23. * documentation, and that the name of Hewlett-Packard and David Betz not be
  24. * used in advertising or publicity pertaining to distribution of the software
  25. * without specific, written prior permission.  Hewlett-Packard and David Betz
  26. * make no representations about the suitability of this software for any
  27. * purpose. It is provided "as is" without express or implied warranty.
  28. *
  29. * HEWLETT-PACKARD AND DAVID BETZ DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
  30. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
  31. * IN NO EVENT SHALL HEWLETT-PACKARD NOR DAVID BETZ BE LIABLE FOR ANY SPECIAL,
  32. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  33. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  34. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  35. * PERFORMANCE OF THIS SOFTWARE.
  36. *
  37. * See ./winterp/COPYRIGHT for information on contacting the authors.
  38. * Please send modifications, improvements and bugfixes to mayer@hplabs.hp.com
  39. * Post XLISP-specific questions/information to the newsgroup comp.lang.lisp.x
  40. *
  41. ********************************************************************************
  42. */
  43. static char rcs_identity[] = "@(#)$Header: wl.c,v 1.1 91/03/14 02:51:36 mayer Exp $";
  44.  
  45. #include <stdio.h>
  46. #include <sys/types.h>
  47. #include <sys/socket.h>
  48. #include <sys/un.h>
  49. #include "../src-server/config.h"
  50.  
  51. char *progname = NULL;
  52.  
  53.  
  54. /*******************************************************************************
  55.  * Send <message> to the server via socket <s>
  56.  *******************************************************************************/
  57. void Server_Send(s, message)
  58.      int s;
  59.      char *message;
  60. {
  61.   if (send(s, message, strlen(message), 0) < 0) {
  62.     perror(progname);
  63.     fprintf(stderr, "%s: unable to send() on Unix Domain Socket.\n", progname);
  64.     exit(1);
  65.   }
  66. }
  67.  
  68.  
  69. /*******************************************************************************
  70.  * Establish a connection with server process, returning socket descriptor
  71.  * if successful.
  72.  *
  73.  * This is an AF_UNIX version of wl.c:Server_Connect()
  74.  *******************************************************************************/
  75.  
  76. int Server_Connect(socket_path)
  77.      char *socket_path;
  78. {
  79.   int s;
  80.   struct sockaddr_un peeraddr_un;
  81.  
  82.   peeraddr_un.sun_family = AF_UNIX;
  83.  
  84.   /*
  85.    * Set the file system entry used for the AF_UNIX Domain Socket.
  86.    */
  87. #ifndef SOCKADDR_UN_MAXLEN
  88. #define SOCKADDR_UN_MAXLEN 108    /* can't find SOCKADDR_UN_MAXLEN on hpux 7.0, however "char sun_path[108];" */ 
  89. #endif
  90.   if (strlen(socket_path) > (SOCKADDR_UN_MAXLEN - 1)) {
  91.     fprintf(stderr, "%s: Unix Domain Socket filepath %s must be shorter than %d bytes.\n",
  92.         progname,
  93.         socket_path,
  94.         SOCKADDR_UN_MAXLEN - 1);
  95.     exit(1);
  96.   }
  97.   else
  98.     strcpy(peeraddr_un.sun_path, socket_path);
  99.  
  100.   /*
  101.    * Create the socket.
  102.    */
  103.   if ((s = socket(peeraddr_un.sun_family, SOCK_STREAM, 0)) == -1) {
  104.     perror(progname);
  105.     fprintf(stderr, "%s: socket() failed to create Unix Domain Socket %s .\n",
  106.         progname, socket_path);
  107.     exit(1);
  108.   }
  109.   
  110.   /*
  111.    * Try to connect to the local server through the socket
  112.    * which was just built into peeraddr.
  113.    */
  114.   if (connect(s, &peeraddr_un, sizeof(peeraddr_un.sun_family) + strlen(peeraddr_un.sun_path)) == -1) {
  115.     perror(progname);
  116.     fprintf(stderr, "%s: unable to connect() to remote via Unix Domain Socket %s .\n",
  117.         progname, socket_path);
  118.     exit(1);
  119.   }
  120.   
  121.   return (s);
  122. }
  123.  
  124.  
  125. /******************************************************************************
  126.  * Server_Disconnect -- shutdown the connection for further sends. This causes
  127.  * the server to receive an EOF condition after it has received all data from
  128.  * Server_Send().
  129. ******************************************************************************/
  130. void Server_Disconnect(s)
  131.      int s;
  132. {
  133.   char buffer[200];
  134.  
  135.   if (shutdown(s, 1) == -1) {
  136.     perror(progname);
  137.     fprintf(stderr, "%s: unable to shutdown() Unix Domain Socket.\n", progname);
  138.     exit(1);
  139.   }
  140. }
  141.  
  142.  
  143. /******************************************************************************
  144.  * AF_UNIX version of wl.c:main()
  145.  ******************************************************************************/
  146. main(argc, argv)
  147.      int argc;
  148.      char *argv[];
  149. {
  150.   extern int getopt();        /* proc to get option letter from argv */
  151.   extern char* optarg;        /* arg string being pointed to by getopt() */
  152.   extern int optind;        /* index into argv set by getopt() */
  153.   int opt;            /* option argument */
  154.   char* socket_path = NULL;    /* for pointing to environment vars */
  155.   int s;            /* connected socket desriptor */
  156.  
  157.   progname = argv[0];
  158.  
  159.   while ((opt = getopt(argc, argv, "f:")) != EOF)
  160.     switch (opt) {
  161.     case 'f':
  162.       socket_path = (char*) malloc((unsigned) strlen(optarg) + 1);
  163.       strcpy(socket_path, optarg);
  164.       break;
  165.     case '?':
  166.       fprintf(stderr, "usage: %s [-f <unix-socket-filepath>] [sexpr]\n", progname);
  167.       fprintf(stderr, "\t<unix-socket-filepath> defaults to %s\n", DEFAULT_UNIX_SOCKET_FILEPATH); /* DEFAULT_UNIX_SOCKET_FILEPATH in "../src-server/config.h" */
  168.       exit(1);
  169.       break;
  170.     }
  171.  
  172.   if (socket_path == NULL)
  173.     if ((socket_path = (char *) getenv(DEFAULT_UNIX_SOCKET_FILEPATH_ENVVAR)) == NULL) /* DEFAULT_UNIX_SOCKET_FILEPATH_ENVVAR in "../src-server/config.h" */
  174.       socket_path = DEFAULT_UNIX_SOCKET_FILEPATH;
  175.  
  176.   s = Server_Connect(socket_path);
  177.     
  178.   /*
  179.    * note that due to a bug/feature of winterp, we must send it only
  180.    * one sexp. Here, in the case that multiple args get passed, we also
  181.    * want to ensure that they're whitespace separated for tokenization
  182.    */
  183.   for (; optind < argc; optind++) {
  184.     Server_Send(s, argv[optind]);
  185.     Server_Send(s, " ");
  186.   }
  187.  
  188.   Server_Disconnect(s);
  189.   exit(0);
  190. }
  191.